package com.hp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hp.context.Config;
import com.hp.dao.MessageDao;
import com.hp.entity.Message;
import com.hp.result.DataGridResultInfo;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.service.MessageService;
import com.hp.utils.UUIDBuild;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageDao messageDao;

    @Transactional(readOnly = true)
    @Override
    public DataGridResultInfo findAll(int page, int rows) throws Exception {
        Page<Message> startPage = PageHelper.startPage(page, rows);
        messageDao.findAll();
        DataGridResultInfo dataGridResultInfo = new DataGridResultInfo();
        dataGridResultInfo.setRows(startPage.getResult());
        dataGridResultInfo.setTotal((int) startPage.getTotal());
        return dataGridResultInfo;
    }

    @Override
    public void addmessage(Message message) throws Exception {
        message.setId(UUIDBuild.getUUID());
        messageDao.add(message);
    }

    @Override
    public SubmitResultInfo deletemsg(String id) throws Exception {
        messageDao.delete(id);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE, 662, new Object[]{"消息"}));
    }

    @Override
    public SubmitResultInfo updatestatus(Message message) throws Exception {
        messageDao.updatestatus(message);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE, 666, new Object[]{"消息"}));
    }
   @Transactional(readOnly = true)
    @Override
    public List<Message> findAllByIsY() throws Exception {
        return messageDao.findByStatusIsY();
    }
    @Transactional(readOnly = true)
    @Override
    public Message findById(String id) throws Exception {
        return messageDao.findById(id);
    }

}
